<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_Api_Core extends Core_Api_Abstract {
	// image (main)
	const IMAGE_WIDTH = 720;
	const IMAGE_HEIGHT = 720;
	
	// image (thumb.normal)
	const THUMB_WIDTH = 140;
	const THUMB_HEIGHT = 160;
	
	// image (thumb.icon)
	const ICON_WIDTH = 48;
	const ICON_HEIGHT = 48;
	
	// image (thumb.profile)
	const PROFILE_WIDTH = 200;
	const PROFILE_HEIGHT = 400;
	
	/**
	 * Gets a paginator for approved - published cmspages
	 *
	 * @param Core_Model_Item_Abstract $user The user to get the messages for
	 * @return Zend_Paginator
	 */
	public function getPublishedCmspagesPaginator($params = array(), $options = null) {
		$params ['published'] = 1;
		return $this->getCmspagesPaginator ( $params, $options );
	}
	
	// Select
	/**
	 * Gets a paginator for cmspages
	 *
	 * @param Core_Model_Item_Abstract $user The user to get the messages for
	 * @return Zend_Paginator
	 */
	public function getCmspagesPaginator($params = array(), $options = null) {
		$paginator = Zend_Paginator::factory ( $this->getCmspagesSelect ( $params, $options ) );
		if (! empty ( $params ['page'] )) {
			$paginator->setCurrentPageNumber ( $params ['page'] );
		}
		if (! empty ( $params ['limit'] )) {
			$paginator->setItemCountPerPage ( $params ['limit'] );
		}
		return $paginator;
	}
	
	/**
	 * Gets a select object for the user's cmspage entries
	 *
	 * @param Core_Model_Item_Abstract $user The user to get the messages for
	 * @return Zend_Db_Table_Select
	 */
	public function getCmspagesSelect($params = array(), $options = null) {
		$table = Engine_Api::_ ()->getDbtable ( 'cmspages', 'cmspage' );
		$rName = $table->info ( 'name' );
		
		$tmTable = Engine_Api::_ ()->getDbtable ( 'TagMaps', 'core' );
		$tmName = $tmTable->info ( 'name' );
		
		$searchTable = Engine_Api::_ ()->fields ()->getTable ( 'cmspage', 'search' )->info ( 'name' );
		
		if (! empty ( $params ['order'] ) && $params ['order'] == 'random') {
			$order_expr = new Zend_Db_Expr ( 'RAND()' );
		} else {
			$order = $rName . '.' . (! empty ( $params ['order'] ) ? $params ['order'] : 'creation_date');
			$order_direction = ! empty ( $params ['order_direction'] ) ? $params ['order_direction'] : 'DESC';
			$order_expr = "$order $order_direction";
		}
		
		if (! empty ( $params ['pre_order'] )) {
			$pre_orders = array (1 => array ("{$rName}.sticky DESC" ), // Sponsored cmspages, then user preference",
2 => array ("{$rName}.sticky DESC", "{$rName}.featured DESC" ), // "Sponsored cmspages, featured cmspages, then user preference",
3 => array ("{$rName}.featured DESC" ), // "Featured cmspages, then user preference",
4 => array ("{$rName}.featured DESC", "{$rName}.sticky DESC" ) )// "Featured cmspages, sticky cmspages, then user preference",
;
			if (isset ( $pre_orders [$params ['pre_order']] )) {
				$order_expr = array_merge ( $pre_orders [$params ['pre_order']], array ($order_expr ) );
			}
		}
		
		// print_r($order_expr);
		

		$select = $table->select ()->order ( $order_expr );
		
		// Build search part of query
		$searchParts = Engine_Api::_ ()->fields ()->getSearchQuery ( 'cmspage', $params );
		if (! empty ( $searchParts )) {
			$select = $select->setIntegrityCheck ( false )->from ( $rName )->joinLeft ( $searchTable, "$searchTable.item_id = $rName.cmspage_id" )->group ( "$rName.cmspage_id" );
			foreach ( $searchParts as $k => $v ) {
				$select = $select->where ( "`{$searchTable}`.{$k}", $v );
			}
		}
		
		if (! empty ( $params ['user_id'] ) && is_numeric ( $params ['user_id'] )) {
			$select->where ( $rName . '.owner_id = ?', $params ['user_id'] );
		}
		
		if (! empty ( $params ['user'] ) && $params ['user'] instanceof User_Model_User) {
			$select->where ( $rName . '.owner_id = ?', $params ['user_id']->getIdentity () );
		}
		
		if (! empty ( $params ['users'] )) {
			$str = ( string ) (is_array ( $params ['users'] ) ? "'" . join ( "', '", $params ['users'] ) . "'" : $params ['users']);
			$select->where ( $rName . '.owner_id in (?)', new Zend_Db_Expr ( $str ) );
		}
		
		if (! empty ( $params ['username'] )) {
			$userTable = Engine_Api::_ ()->getDbtable ( 'users', 'user' )->info ( 'name' );
			
			$select = $select->setIntegrityCheck ( false )->from ( $rName )->joinLeft ( $userTable, "$userTable.user_id = $rName.owner_id" )->where ( $userTable . '.username LIKE ?', '%' . $params ['username'] . '%' );
		}
		
		if (! empty ( $params ['tag'] )) {
			$select = $select->setIntegrityCheck ( false )->from ( $rName )->joinLeft ( $tmName, "$tmName.resource_id = $rName.cmspage_id" )->where ( $tmName . '.resource_type = ?', 'cmspage' )->where ( $tmName . '.tag_id = ?', $params ['tag'] );
		}
		
		if (! empty ( $params ['category'] )) {
			$select->where ( $rName . '.category_id = ?', $params ['category'] );
		}
		
		// Could we use the search indexer for this?
		if (! empty ( $params ['search'] )) {
			$select->where ( $rName . ".title LIKE ? OR " . $rName . ".description LIKE ?", '%' . $params ['search'] . '%' );
		}
		
		if (! empty ( $params ['start_date'] )) {
			$select->where ( $rName . ".creation_date > ?", date ( 'Y-m-d', $params ['start_date'] ) );
		}
		
		if (! empty ( $params ['end_date'] )) {
			$select->where ( $rName . ".creation_date < ?", date ( 'Y-m-d', $params ['end_date'] ) );
		}
		
		$bool_keys = array ('published', 'featured', 'sticky' );
		foreach ( $bool_keys as $bool_key ) {
			if (isset ( $params [$bool_key] ) && strlen ( $params [$bool_key] )) {
				$select->where ( $rName . '.' . $bool_key . ' = ?', $params [$bool_key] );
			}
		}
		
		if (isset ( $params ['exclude_cmspage_ids'] ) and ! empty ( $params ['exclude_cmspage_ids'] )) {
			$select->where ( $rName . '.cmspage_id NOT IN (?)', $params ['exclude_cmspage_ids'] );
		}
		
		// Engine_Api::_()->getApi('debug','radcodes')->log($params,'getCmspagesSelect params');
		// Engine_Api::_()->getApi('debug','radcodes')->log($select->__toString(),'getCmspagesSelect select query');
		

		return $select;
	}
	
	public function getCategories() {
		$table = Engine_Api::_ ()->getDbtable ( 'categories', 'cmspage' );
		//$table = $this->api()->getDbtable('categories', 'cmspage');
		$categories = $table->fetchAll ( $table->select ()->order ( 'order' ) );
		
		return $categories;
	}
	
	public function getCategory($category_id) {
		static $categories = array ();
		
		if (! isset ( $categories [$category_id] )) {
			$categories [$category_id] = Engine_Api::_ ()->getDbtable ( 'categories', 'cmspage' )->find ( $category_id )->current ();
		}
		
		return $categories [$category_id];
	}
	
	public function getUserCategories($user_id) {
		$table = Engine_Api::_ ()->getDbtable ( 'categories', 'cmspage' );
		$uName = Engine_Api::_ ()->getDbtable ( 'cmspages', 'cmspage' )->info ( 'name' );
		$iName = $table->info ( 'name' );
		
		$select = $table->select ()->setIntegrityCheck ( false )->from ( $iName, array ('category_name' ) )->joinLeft ( $uName, "$uName.category_id = $iName.category_id" )->group ( "$iName.category_id" )->where ( $uName . '.owner_id = ?', $user_id );
		
		return $table->fetchAll ( $select );
	}
	
	public function convertCategoriesToArray($categories) {
		$categories_prepared = array ();
		foreach ( $categories as $category ) {
			$categories_prepared [$category->category_id] = $category->category_name;
		}
		return $categories_prepared;
	}
	
	function getArchiveList($params = array()) {
		
		$table = Engine_Api::_ ()->getDbtable ( 'cmspages', 'cmspage' );
		$rName = $table->info ( 'name' );
		
		$select = $table->select ()->from ( $rName, array ("DATE_FORMAT(creation_date, '%Y-%m') as period", "COUNT(*) as total" ) )->group ( 'period' )->order ( "period DESC" );
		
		if (! empty ( $params ['user_id'] ) && is_numeric ( $params ['user_id'] )) {
			$select->where ( $rName . '.owner_id = ?', $params ['user_id'] );
		}
		if (isset ( $params ['published'] ) && strlen ( $params ['published'] )) {
			$select->where ( $rName . '.published = ?', $params ['published'] );
		}
		
		$stmt = $select->query ();
		$results = $stmt->fetchAll ();
		
		return $results;
	}
	
	public function createPhoto($params, $photo) {
		if ($photo instanceof Storage_Model_File) {
			$params ['file_id'] = $photo->getIdentity ();
		} 

		else {
			//Engine_Api::_()->getApi('debug','radcodes')->log($params,'createPhoto $params=');
			//Engine_Api::_()->getApi('debug','radcodes')->log($photo,'createPhoto $photo=');
			

			if ($photo instanceof Zend_Form_Element_File) {
				$file = $photo->getFileName ();
			} else if (is_array ( $photo ) && ! empty ( $photo ['tmp_name'] )) {
				$file = $photo ['tmp_name'];
			} else if (is_string ( $photo ) && file_exists ( $photo )) {
				$file = $photo;
			} else {
				throw new Cmspage_Model_Exception ( 'invalid argument passed to Cmspage_Api_Core::createPhoto' );
			}
			
			if (is_array ( $photo ) && ! empty ( $photo ['name'] )) {
				$name = basename ( $photo ['name'] );
			} else {
				$name = basename ( $file );
			}
			
			$path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
			
			$mainName = $path . '/m_' . $name;
			$profileName = $path . '/p_' . $name;
			$thumbName = $path . '/t_' . $name;
			$iconName = $path . '/is_' . $name;
			
			$image = Engine_Image::factory ();
			$image->open ( $file )->resize ( self::IMAGE_WIDTH, self::IMAGE_HEIGHT )->write ( $mainName )->destroy ();
			
			$image = Engine_Image::factory ();
			$image->open ( $file )->resize ( self::PROFILE_WIDTH, self::PROFILE_HEIGHT )->write ( $profileName )->destroy ();
			
			$image = Engine_Image::factory ();
			$image->open ( $file )->resize ( self::THUMB_WIDTH, self::THUMB_HEIGHT )->write ( $thumbName )->destroy ();
			
			// Resize image (icon)
			$image = Engine_Image::factory ();
			$image->open ( $file );
			
			$size = min ( $image->height, $image->width );
			$x = ($image->width - $size) / 2;
			$y = ($image->height - $size) / 2;
			
			$image->resample ( $x, $y, $size, $size, self::ICON_WIDTH, self::ICON_HEIGHT )->write ( $iconName )->destroy ();
			
			// Store photos
			$photo_params = array ('parent_id' => $params ['cmspage_id'], 'parent_type' => 'cmspage' );
			
			$photoFile = Engine_Api::_ ()->storage ()->create ( $mainName, $photo_params );
			$profileFile = Engine_Api::_ ()->storage ()->create ( $profileName, $photo_params );
			$thumbFile = Engine_Api::_ ()->storage ()->create ( $thumbName, $photo_params );
			$iconFile = Engine_Api::_ ()->storage ()->create ( $iconName, $photo_params );
			
			$photoFile->bridge ( $profileFile, 'thumb.profile' );
			$photoFile->bridge ( $thumbFile, 'thumb.normal' );
			$photoFile->bridge ( $iconFile, 'thumb.icon' );
			
			$params ['file_id'] = $photoFile->file_id;
			//$params['photo_id'] = $photoFile->file_id;
			

			// Remove temp files
			@unlink ( $mainName );
			@unlink ( $profileName );
			@unlink ( $thumbName );
			@unlink ( $iconName );
		
		}
		
		$row = Engine_Api::_ ()->getDbtable ( 'photos', 'cmspage' )->createRow ();
		$row->setFromArray ( $params );
		$row->save ();
		return $row;
	}
	
	public function getPopularTags($options = array()) {
		$tags = Engine_Api::_ ()->radcodes ()->getPopularTags ( 'cmspage', $options );
		return $tags;
	}
	
	public function clearCmspageHome($user_id) {
		$params = array ('user_id' => $user_id, 'home' => 1 );
		$cmspages = $this->getCmspagesPaginator ( $params );
		if ($cmspages->getTotalItemCount ()) {
			foreach ( $cmspages as $cmspage ) {
				$cmspage->home = 0;
				$cmspage->save ();
			}
		}
	
	}

}